from codesters.demo import Demo
class newDemo(Demo):
def create_continue_button(self):
text = "Continue"
background_sprite = codesters.Rectangle(-203, -219, 125, 62.5, "green", "black")
button = self.create_button(text, -197, -220, True, "white", "#07A7E0", 90, 200, "white")
self.stage.remove_sprite(button.background_sprite)
button.background_sprite = background_sprite
self.add_hover_opacity_element(button)
return button
demo = newDemo()
wait_time = 1
stage_shade = codesters.Square(0, 0, 500, "#ECECEC")
stage.set_background_color("black")
nar = codesters.Sprite("person12", 0, -150)
nar.say("Let's talk about how this fireworks program will work!")
demo.continue_action()
nar.say("")
nar_text = codesters.Text("There are 3 main steps:\n\n\n\n\n\n", 0, 50)
nar_text.set_text("There are 3 main steps:\n\n1) Set up the information needed to make fireworks\n\n\n\n")
stage.wait(wait_time*3)
nar_text.set_text("There are 3 main steps:\n\n1) Set up the information needed to make fireworks\n\n2) Create the particles that make up the firework\n\n")
stage.wait(wait_time*3)
nar_text.set_text("There are 3 main steps:\n\n1) Set up the information needed to make fireworks\n\n2) Create the particles that make up the firework\n\n3) Move the particles outward in a burst!")
stage.wait(wait_time*3)
demo.continue_action()
nar_text.set_text("")
nar.say("Let's learn more about each step!")
opacity = 1
while opacity > 0:
opacity -= .05
stage_shade. set_opacity(opacity)
if round(opacity, 2) == .25:
nar.set_say_color("white")
stage.wait(0.05)
stage.wait(wait_time*1)
nar.say("")
nar_text.set_color("white")
nar_text.set_text("For fireworks, we need dark!\n\n\n\n")
stage.wait(wait_time*2)
nar_text.set_text("For fireworks, we need dark!\n\nWe also need some information, like what color the fireworks should be, and how many lines each firework should be made out of!")
stage.wait(wait_time*3)
demo.continue_action()
nar_text.set_text("Look at the code editor to the left.\n\nYou can see the code setting up that information on lines 19 to 22.")
stage.wait(wait_time*4)
demo.continue_action()
nar_text.hide()
def return_trajectory(speed, angle):
radians = math.radians(angle)
x_velocity = speed * math.cos(radians)
y_velocity = speed * math.sin(radians)
return (x_velocity, y_velocity)
def firework(x, y, wait):
particles = []
for color in colors:
for counter in range(amount):
c = codesters.Circle(x, y, 10, color)
particles.append(c)
c.set_say_color(color)
if wait != 0:
c.say(counter+1)
stage.wait(wait)
c.say("")
# stage.wait(.2)
return particles
def move(particles, wait = 0.5):
angle = 0
positions = []
for count, p in enumerate(particles):
count += 1
x_speed = return_trajectory(speed, angle)[0]
y_speed = return_trajectory(speed, angle)[1]
p.set_x_speed(x_speed)
p.set_y_speed(y_speed)
angle += 360/amount
if count % amount == 0:
stage.wait(.1)
stage.wait(wait)
for p in particles:
# stage.remove_sprite(p)
p.set_x_speed(0)
p.set_y_speed(0)
positions.append((p.get_x(), p.get_y()))
return positions
colors = ["purple", "yellow", "red"]
amount = 10
speed = 5
button1 = demo.create_button("Click Here", 0, 50, True, "white", "#F48F3B", 100, 50, "")
button1.event_click(demo.return_paused_click())
demo.pause()
button1.hide()
nar_text.set_text("This creates the firework one particle at a time! Each particle is created on top of the others.\n\n\n\n\n\n")
nar_text.show()
firework(0, 50, 0.5)
demo.continue_action()
demo.remove_all_sprites_except([nar, nar_text])
nar_text.set_text("Whoa! We created the particles for the firework by creating 10 particles per color.\n\n\n\n")
nar_text.show()
stage.wait(wait_time*2)
nar_text.set_text("Whoa! We created the particles for the firework by creating 10 particles per color.\n\nThat is what code lines 33-36 are doing!\n\n")
stage.wait(wait_time*4)
# # ask - how many particles did we just create?
nar.say("Time for a test!")
stage.wait(1.5)
answer = 30
how_many = False
while type(how_many) != int:
try:
how_many = int(nar.ask("Look at lines 33 and 34 in the code. How many particles does this code create in total?"))
break
except:
nar.say("Enter a number!", 2)
test = True
while test is True:
if how_many == answer:
test = False
else:
if how_many < answer:
nar.say("Hm... Try again!")
stage.wait(2.5)
nar.say("Try a larger number...")
if how_many > answer:
nar.say("Hm... Try again!")
stage.wait(2.5)
nar.say("Try a smaller number...")
how_many = int(nar.ask("Look at lines 33 and 34 in the code. How many particles does this code create in total?"))
nar.say("")
stage.wait(wait_time)
nar_text.set_text("Normally, this happens so fast, it looks like only 1 particle is created!\n\n\n\n\n\n")
button1 = demo.create_button("Click Here", 0, 50, True, "white", "#F48F3B", 100, 50, "")
button1.event_click(demo.return_paused_click())
demo.pause()
button1.hide()
particle_list = firework(0, 50, 0)
nar_text.set_text("That was step 2!\nNow we need to move the particles outward!\n\n\n\n\n")
stage.wait(wait_time*2)
demo.continue_action()
nar_text.set_text("To do that, we will need to calculate an x-speed (horizontal) and y-speed (vertical) for each particle.\n\n\n")
demo.continue_action()
nar_text.hide()
positions1 = move(particle_list, .7)
stage.wait(wait_time*2)
nar.say("Awesome! I used trigonometry, or math that deals with sides and angles of triangles, to work that out.")
stage.wait(wait_time*4)
angle = 0
for counter in range(10):
radius = 250
circle = codesters.Circle(0, 0, radius*2, None, "white")
circle.set_line_thickness(3)
circle.set_y(circle.get_y()+50)
radius_line = codesters.Line(0, 0, radius*math.cos(math.radians(angle)), radius*math.sin(math.radians(angle)), "red")
radius_line.set_y(radius_line.get_y()+50)
adj_side = codesters.Line(0, 0, radius*math.cos(math.radians(angle)), 0, "blue")
adj_side.set_y(adj_side.get_y()+50)
opp_side = codesters.Line(radius*math.cos(math.radians(angle)), 0, radius*math.cos(math.radians(angle)), radius*math.sin(math.radians(angle)), "green")
opp_side.set_y(opp_side.get_y()+50)
angle += 360 / 10
demo.continue_action()
demo.remove_all_sprites_except([nar, nar_text]+particle_list)
nar.say("")
move(particle_list)
nar.say("Oops, what happened?!\n\n")
stage.wait(wait_time*2)
nar.say("Oops, what happened?!\n\nThe particles bounced when they hit a wall! I know how to fix that...")
stage.wait(wait_time*2)
demo.continue_action()
for p in particle_list:
p.hide()
for index, p in enumerate(particle_list):
p.set_rotation(0)
p.go_to(positions1[index][0], positions1[index][1])
for p in particle_list:
p.show()
stage.disable_all_walls()
nar.say("I need to disable the walls!")
demo.continue_action()
nar.say("")
move(particle_list, .7)
demo.remove_all_sprites_except([nar, nar_text])
nar.say("That was cool! Ready to try yourself?\n\n\n")
stage.wait(wait_time*3)
nar.say("That was cool! Ready to try yourself?\n\nClick anywhere on the stage to make a firework.\n")
stage.wait(wait_time*3)
nar.say("That was cool! Ready to try yourself?\n\nClick anywhere on the stage to make a firework.\nThen click Submit and Next to move on!")
stage.wait(wait_time*3)
demo.continue_action()
nar.say("")
####################################################################
# PROVIDED FUNCTION #
####################################################################
### This is the math for step 2! ###
def return_trajectory(speed, angle):
radians = math.radians(angle)
x_velocity = speed * math.cos(radians)
y_velocity = speed * math.sin(radians)
return (x_velocity, y_velocity)
####################################################################
### Step 1! This is the information for how many lines in each
### firework, the colors, and how fast each particle should move!
stage.set_background_color("black")
amount = 10
speed = 5
color_list = ["purple", "yellow", "red"]
stage.disable_all_walls()
### EVERYTHING ELSE takes place inside this click event
def click():
x = stage.click_x()
y = stage.click_y()
### Step 2: Make the firework! ###
particle_list = []
for color in color_list:
for counter in range(amount):
# sprite = codesters.Circle(x, y, diameter, "color")
sprite = codesters.Circle(x, y, 10, color)
particle_list.append(sprite)
### Step 3: Make the firework burst outward!
angle = 0
for count, p in enumerate(particle_list):
count += 1
speed_values = return_trajectory(speed, angle)
p.set_x_speed(speed_values[0])
p.set_y_speed(speed_values[1])
angle += 360/amount
if count % amount == 0:
stage.wait(.1)
stage.event_click(click)
tester = TestManager()
tester.display_success_message("Great job! Click Submit and Next to move on!")